package tsli.acm.biz;

import java.text.DecimalFormat;

import tsli.acm.dao.SFCAgentStructureDAO;
import tsli.acm.dao.SFCCommissionNegativeDAO;
import tsli.acm.dao.SFCCommissionPositiveDAO;
import tsli.acm.database.Database;

public class RAComponentAPIMethod {

	public double CalRA (String vaDate, String vaRecruiterId) {
		DecimalFormat df = new DecimalFormat("#,##0.00");
		Database db = new Database();
		SFCAgentStructureDAO voSFCASManagement = new SFCAgentStructureDAO(db);
		SFCCommissionPositiveDAO voSFCComPosManagement = new SFCCommissionPositiveDAO(db);
		SFCCommissionNegativeDAO voSFCComNegManagement = new SFCCommissionNegativeDAO(db);
		String[][] vaFYPForFreelook = voSFCComNegManagement.findFYPForCalRA(vaDate);
		String[] vaEmployeeId = voSFCASManagement.findNewEmployee(vaRecruiterId, vaDate);
		double[] vnFYP = new double[6];
		// calculate ทุกๆเดือนของ employee แต่ละคน
		for (int y = 0; y < vaEmployeeId.length; y++) {
			// 0 = CLOS_YM, 1 = FYP, 2 = PO_NO, 3 = PE_NO_COL, 4 = FST_ENCO_MMS
			String[][] vaSumFYPInEachMonth = voSFCComPosManagement.findFYPForCalRA(vaEmployeeId[y], vaDate);
			// ถ้า 201411 - 201411 = 0 คือเดือน 6
			for(int z = 0; z < vaSumFYPInEachMonth.length; z++) {
				int vnMMS = Integer.parseInt(vaSumFYPInEachMonth[z][4]);
				for (int q = 0; q < vaFYPForFreelook.length; q++) {
					if (vaFYPForFreelook[q][2].equals(vaSumFYPInEachMonth[z][2])) {
						if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 0) {
							vnFYP[vnMMS - 1] -= Double.parseDouble(vaFYPForFreelook[q][1]);
						// 201411 - 201410 = 1 คือเดือน 5
						} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 1) {
							vnFYP[vnMMS - 2] -= Double.parseDouble(vaFYPForFreelook[q][1]);
						} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 2) {
							vnFYP[vnMMS - 3] -= Double.parseDouble(vaFYPForFreelook[q][1]);
						} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 3) {
							vnFYP[vnMMS - 4] -= Double.parseDouble(vaFYPForFreelook[q][1]);
						} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 4) {
							vnFYP[vnMMS - 5] -= Double.parseDouble(vaFYPForFreelook[q][1]);
						} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 5) {
							vnFYP[vnMMS - 6] -= Double.parseDouble(vaFYPForFreelook[q][1]);
						}
					}
				}
				if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 0) {
					vnFYP[vnMMS - 1] += Double.parseDouble(vaSumFYPInEachMonth[z][1]);
				// 201411 - 201410 = 1 คือเดือน 5
				} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 1) {
					vnFYP[vnMMS - 2] += Double.parseDouble(vaSumFYPInEachMonth[z][1]);
				} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 2) {
					vnFYP[vnMMS - 3] += Double.parseDouble(vaSumFYPInEachMonth[z][1]);
				} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 3) {
					vnFYP[vnMMS - 4] += Double.parseDouble(vaSumFYPInEachMonth[z][1]);
				} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 4) {
					vnFYP[vnMMS - 5] += Double.parseDouble(vaSumFYPInEachMonth[z][1]);
				} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 5) {
					vnFYP[vnMMS - 6] += Double.parseDouble(vaSumFYPInEachMonth[z][1]);
				}				
			}
		}
		
		double vnSum = 0;
		double[] vaShowSum = new double[6];
		String[] vaTmpShowSum = new String[6];
		double[] vaPercent = new double[6];
		boolean vbStatus = false;
		for (int y = 0; y < 6; y++) {
			vnSum += vnFYP[y];
			if (vnSum < 30000) { // ถ้าน้อยกว่า 30,000
				vaShowSum[y] = vnSum;
			} else if (vnSum >= 30000 && vbStatus == false) { // ถ้ามากกว่าหรือเท่ากับ 30,000 ครั้งแรก
				vaShowSum[y] = vnSum;
				vbStatus = true;
			} else { // มากกว่า 30,000
				vaShowSum[y] = -1;
			}
		}
		// set accumulate FYP-C
		for (int y = 0; y < 6; y++) {
			if (vaShowSum[y] == -1) {
				vaTmpShowSum[y] = "";
			} else {
				vaTmpShowSum[y] = df.format(vaShowSum[y]);
			}
		}
		// set percent
		for (int y = 0; y < 6; y++) {
			if (vaShowSum[y] == -1 || vaShowSum[y] >= 30000) {
				vaPercent[y] = 5;
			} else {
				vaPercent[y] = 0;
			}
		}
		// RA to pay
		double[] vnSumRAEachRow = new double[6]; // RA ที่จ่ายแต่ละเดือน
		double vnSumRA = 0; // RA ที่จ่ายรวมทั้งหมด
		for (int y = 0; y < 6; y++) {
			if (vaShowSum[y] == -1) {
				//vnSumRA += vnFYP[y]*0.05;
				vnSumRAEachRow[y] = vnFYP[y]*0.05;
			} else if (vaShowSum[y] >= 30000) {
				vnSumRAEachRow[y] = vaShowSum[y]*0.05;
			} else {
				vnSumRAEachRow[y] = 0;
			}
		}
		// calculate Agent Debt
//		ArrayList<Double> voArrayList = new ArrayList<Double>();
//		for (int y = 0; y < 6; y++) {
//			if (vnSumRAEachRow[y] != 0) {
//				voArrayList.add(vnSumRAEachRow[y]);
//			}
//		}
//		double[] vnTmp = new double[voArrayList.size()];
//		for (int y = 0; y < voArrayList.size(); y++) {
//			vnTmp[y] = voArrayList.get(y);
//		}
//		AgentDebtManagement voADM = new AgentDebtManagement();
//		String[] vaResult = voADM.CalAgentDebt(vaRecruiterId, vnTmp, db);
//		int z = 0;
//		for (int y = 0; y < 6; y++) {
//			if (vnSumRAEachRow[y] != 0) {
//				vnSumRAEachRow[y] = Double.parseDouble(vaResult[z]);
//				vnSumRA += vnSumRAEachRow[y];
//				z++;
//			}
//		}
//		if ((!vaResult[vaResult.length - 1].equals("")) && vaNegative.equals("")) 
//			vaNegative += "<br>หมายเหตุ : ";
//		vaNegative += vaResult[vaResult.length - 1];

		for (int y = 0; y < 6; y++) {
			vnSumRA += vnSumRAEachRow[y];
		}
		db.close();
		return vnSumRA;
	}

	public static void main(String[] args) {
		
		System.out.println("Recruiting Award : RA");
		/* ---start---- เดือน/ปีที่ต้องการคำนวณ RA เช่น '201411' */
		String vaDate = "201411";
		String vaRecruiterId = "221400051";
		RAComponentAPIMethod abc = new RAComponentAPIMethod();
		System.out.println(abc.CalRA(vaDate, vaRecruiterId));
		/*
		// Result variables
		Database db = new Database();
		String[] vaRecruiterName = null;
		SFCAgentStructureDAO voSFCASManagement = new SFCAgentStructureDAO(db);
		SFCCommissionPositiveDAO voSFCComPosManagement = new SFCCommissionPositiveDAO(db);
		SFCCommissionNegativeDAO voSFCComNegManagement = new SFCCommissionNegativeDAO(db);
		String[] vaRecruiterId = voSFCASManagement.findRecruiterIdBetweenOneToSix(vaDate);
		String[][] vaFYPForFreelook = voSFCComNegManagement.findFYPForCalRA(vaDate);
		for (int q = 0; q < vaFYPForFreelook.length; q++)
			System.out.println("TEST123: " + Integer.parseInt(vaFYPForFreelook[q][0]) + " " 
				+ Double.parseDouble(vaFYPForFreelook[q][1]) + " " + vaFYPForFreelook[q][2]);
		for (int i = 0; i < vaRecruiterId.length; i++) {
			// ทำ RA ทีละคน
			// id ของ RA
			System.out.print("คนที่: " + (i+1) + "    รหัส: " + vaRecruiterId[i] + " ");
			System.out.print("          ");
			// name ของ RA
			vaRecruiterName = voSFCASManagement.findNameById(vaRecruiterId[i]);
			System.out.print("ชื่อ: " + vaRecruiterName[0] + " " + vaRecruiterName[1] + " " + vaRecruiterName[2]);
			System.out.print("          ");
			System.out.println();
			// employee ทุกคนใน RA คนนั้น
			String[] vaEmployeeId = voSFCASManagement.findNewEmployee(vaRecruiterId[i], vaDate);
			for (int y = 0; y < vaEmployeeId.length; y++) {
				System.out.println("พนักงาน: " + vaEmployeeId[y]);
			}
			double[] vnFYP = new double[6];
			// calculate ทุกๆเดือนของ employee แต่ละคน
			for (int y = 0; y < vaEmployeeId.length; y++) {
				// 0 = CLOS_YM, 1 = FYP
				String[][] vaSumFYPInEachMonth = voSFCComPosManagement.findFYPForCalRA(vaEmployeeId[y], vaDate);
				// ถ้า 201411 - 201411 = 0 คือเดือน 6
				for(int z = 0; z < vaSumFYPInEachMonth.length; z++) {
					System.out.println("เงินที่ทำได้: " + vaSumFYPInEachMonth[z][3] + " " + Integer.parseInt(vaSumFYPInEachMonth[z][0]) + " " 
							+ Double.parseDouble(vaSumFYPInEachMonth[z][1]) + " " + vaSumFYPInEachMonth[z][2]);
					for (int q = 0; q < vaFYPForFreelook.length; q++) {
						if (vaFYPForFreelook[q][2].equals(vaSumFYPInEachMonth[z][2])) {
							System.out.println("มี negative: " + vaFYPForFreelook[q][2] +
									" จำนวนเงิน: " + Double.parseDouble(vaFYPForFreelook[q][1]) + 
									" ได้มาในเดือน: " + vaSumFYPInEachMonth[z][0] + " ยกเลิกในเดือน: " + vaFYPForFreelook[q][0]);
							if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 0) {
								vnFYP[5] -= Double.parseDouble(vaFYPForFreelook[q][1]);
							// 201411 - 201410 = 1 คือเดือน 5
							} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 1) {
								vnFYP[4] -= Double.parseDouble(vaFYPForFreelook[q][1]);
							} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 2) {
								vnFYP[3] -= Double.parseDouble(vaFYPForFreelook[q][1]);
							} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 3) {
								vnFYP[2] -= Double.parseDouble(vaFYPForFreelook[q][1]);
							} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 4) {
								vnFYP[1] -= Double.parseDouble(vaFYPForFreelook[q][1]);
							} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 5) {
								vnFYP[0] -= Double.parseDouble(vaFYPForFreelook[q][1]);
							}
						}
					}
					if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 0) {
						vnFYP[5] += Double.parseDouble(vaSumFYPInEachMonth[z][1]);
					// 201411 - 201410 = 1 คือเดือน 5
					} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 1) {
						vnFYP[4] += Double.parseDouble(vaSumFYPInEachMonth[z][1]);
					} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 2) {
						vnFYP[3] += Double.parseDouble(vaSumFYPInEachMonth[z][1]);
					} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 3) {
						vnFYP[2] += Double.parseDouble(vaSumFYPInEachMonth[z][1]);
					} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 4) {
						vnFYP[1] += Double.parseDouble(vaSumFYPInEachMonth[z][1]);
					} else if (Integer.parseInt(vaDate) - Integer.parseInt(vaSumFYPInEachMonth[z][0]) == 5) {
						vnFYP[0] += Double.parseDouble(vaSumFYPInEachMonth[z][1]);
					}
				}
			}
			double vnSum = 0;
			double[] vaShowSum = new double[6];
			boolean vbStatus = false;
			DecimalFormat df = new DecimalFormat("#,##0.00");
			System.out.println("เดือนที่    1            2            3            4            5            6            รวม");
			for (int y = 0; y < 6; y++) {
				System.out.print(df.format(vnFYP[y]) + "            ");
				vnSum += vnFYP[y];
				if (vnSum < 30000) {
					vaShowSum[y] = vnSum;
				} else if (vnSum >= 30000 && vbStatus == false) {
					vaShowSum[y] = vnSum;
					vbStatus = true;
				} else {
					vaShowSum[y] = -1;
				}
			}
			System.out.println(df.format(vnSum));
			for (int y = 0; y < 6; y++) {
				if (vaShowSum[y] == -1) {
					System.out.print("            ");
				} else {
					System.out.print(df.format(vaShowSum[y]) + "            ");
				}
			}
			System.out.println();
			for (int y = 0; y < 6; y++) {
				if (vaShowSum[y] == -1 || vaShowSum[y] >= 30000) {
					System.out.print("5%             ");
				} else {
					System.out.print("0%            ");
				}
			}
			System.out.println();
			double vnSumRA = 0;
			for (int y = 0; y < 6; y++) {
				if (vaShowSum[y] == -1 || vaShowSum[y] >= 30000) {
					vnSumRA += vnFYP[y]*0.05;
					System.out.print(df.format(vnFYP[y]*0.05) + "             ");
				} else {
					System.out.print(df.format(0) + "            ");
				}
			}
			System.out.println(df.format(vnSumRA));
			System.out.println();
		}
		db.close();
		*/
	}

}
